package com.ruoyi.goods.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.goods.mapper.BmCategoryMapper;
import com.ruoyi.goods.domain.BmCategory;
import com.ruoyi.goods.service.IBmCategoryService;

/**
 * 商品分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-03
 */
@Service
public class BmCategoryServiceImpl implements IBmCategoryService 
{
    @Autowired
    private BmCategoryMapper bmCategoryMapper;
    @Autowired
    private RedisService redisService;

    /**
     * 查询商品分类
     * 
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public BmCategory selectBmCategoryById(Long id)
    {
        return bmCategoryMapper.selectBmCategoryById(id);
    }

    /**
     * 查询商品分类列表
     * 
     * @param bmCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<BmCategory> selectBmCategoryList(BmCategory bmCategory)
    {
        return bmCategoryMapper.selectBmCategoryList(bmCategory);
    }

    /**
     * 商品一级分类
     * @param
     * @return
     */
    @Override
    public List<BmCategory> selectBmCategoryFirstList(){
        List<BmCategory> firstCategory = redisService.getCacheList("category:firstCategory");
        if (firstCategory==null||firstCategory.size()==0){
            List<BmCategory> firstCategoryList = bmCategoryMapper.selectBmCategoryFirstList();
            redisService.setCacheList("category:firstCategory",firstCategoryList);
            return firstCategoryList;
        }
        return firstCategory;
    }
    /**
     * 商品二级分类
     */
    @Override
    public List<BmCategory> selectBmCategorySecondList(){
        return bmCategoryMapper.selectBmCategorySecondList();
    }

    @Override
    public List<BmCategory> selectBmCategorySecondListByParentId(Integer parentId) {
        List<BmCategory> secondCategory = redisService.getCacheList("category:secondCategory"+parentId);
        if (secondCategory==null||secondCategory.size()==0){
            List<BmCategory> secondCategoryList = bmCategoryMapper.selectBmCategorySecondListByParentId(parentId);
            redisService.setCacheList("category:secondCategory"+parentId,secondCategoryList);
            return secondCategoryList;
        }
        return secondCategory;
    }

    /**
     * 新增商品分类
     * 
     * @param bmCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertBmCategory(BmCategory bmCategory)
    {
        bmCategory.setCreateTime(DateUtils.getNowDate());
        return bmCategoryMapper.insertBmCategory(bmCategory);
    }

    /**
     * 修改商品分类
     * 
     * @param bmCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateBmCategory(BmCategory bmCategory)
    {
        return bmCategoryMapper.updateBmCategory(bmCategory);
    }

    /**
     * 批量删除商品分类
     * 
     * @param ids 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteBmCategoryByIds(Long[] ids)
    {
        return bmCategoryMapper.deleteBmCategoryByIds(ids);
    }

    /**
     * 删除商品分类信息
     * 
     * @param id 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteBmCategoryById(Long id)
    {
        return bmCategoryMapper.deleteBmCategoryById(id);
    }

    @Override
    public List<BmCategory> getTreeList() {
        //获取父级集合
        List<BmCategory> list = bmCategoryMapper.getByParentId(0L);
        //调用生成树
        generateList(list);
        return list;
    }
    //递归生成树形结构
    public void generateList(List<BmCategory> list){
        for (int i = 0; i < list.size(); i++) {
            //获得某一条数据
            BmCategory category = list.get(i);
            //获得该数据的id
            long id = category.getId();
            //通过id得到子节点集合
            List<BmCategory> child = bmCategoryMapper.getByParentId(id);
            //在对象中新增数据
            category.setChild(child);
            //递归生成树
            generateList(child);
        }
    }
}
